package com.ex.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ex.model.TStudent;
import com.ex.service.StudentService;

@Controller
@RequestMapping(value = "/student/")
public class StudentController {
	@Autowired
	private StudentService studentService;

	@RequestMapping(value = "login")
	@ResponseBody
	public Object login(HttpServletRequest request, HttpSession session,
			@RequestParam(value = "callback", required = false) String callback) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String code = request.getParameter("code");
		String psd = request.getParameter("password");

		TStudent student = new TStudent();
		student.setCode(code);
		student.setPassword(psd);

		if ("".equals(code) || "".equals(psd)) {
			return "";
		}

		List<TStudent> result = studentService.query(student);

		if (result.size() > 0) {
			session.setAttribute("teacher", result);
			resultMap.put("errorcode", 0);
		} else {
			resultMap.put("errorcode", 1);
			resultMap.put("errormessage", "用户名或密码不存在");
		}

		MappingJacksonValue mapping = new MappingJacksonValue(resultMap);
		// 设置前端的回调函数
		mapping.setJsonpFunction(callback);
		return mapping;
	}
}
